package com.ieei.serviceImpl;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import javax.jws.WebService;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import com.ieei.iService.HelloWorld;
import com.ieei.server.User;

@WebService(endpointInterface = "com.ieei.iService.HelloWorld", serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {
	@Resource(name="jmsTemplate")
	private JmsTemplate jmsTemplate;
	/**
	 * 要发送的目的地
	 */
	@Resource(name="requestDestination")
	private Destination requestDestination;
	
	Map<Integer, User> users = new LinkedHashMap<Integer, User>();

	public String sayHi(String text) {
		System.out.println("sayHi called");
		jmsTemplate.send(requestDestination, new MessageCreator() {

			@Override
			public Message createMessage(Session session) throws JMSException {
				//创建文本message
				Message msg = session.createTextMessage(text);
				//创建mapMessage
				MapMessage map=session.createMapMessage();
				map.setLong("long", 1);
				map.setObject("object", "我是object");
				return map;
			}
		});
		return "Hello " + text;
	}

	public String sayHiToUser(User user) {
		System.out.println("sayHiToUser called");
		users.put(users.size() + 1, user);
		return "Hello " + user.getName();
	}

	public Map<Integer, User> getUsers() {
		System.out.println("getUsers called");
		return users;
	}

}
